﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace VXCompany.SilverlightLogging.MVVM.Models
{
	public class NotifyingBase : INotifyPropertyChanged
	{
		/// <summary>
		/// Event raised when a property of the object changed
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		///	Notifies subscribers that a property with the given name has changed.
		/// </summary>
		/// <param name="propertyName">The name of the property that changed</param>
		protected void RaisePropertyChanged(string propertyName)
		{
			this.VerifyPropertyName(propertyName);

			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		/// <summary>
		///	Verifies that the given property name actually exists as a public property on this ViewModel.
		/// </summary>
		/// <param name="propertyName">The name of the property to verify</param>
		/// <exception cref="Exception">Thrown when, in debug mode, a given propertyname could not be found using reflection.</exception>
		[Conditional("DEBUG")]
		[DebuggerStepThrough]
		public void VerifyPropertyName(string propertyName)
		{
			// Check to see if the property exists
			if (GetType().GetProperty(propertyName) == null)
			{
				// It seems it doesn't. Alert the user by throwing a nice exception
				string msg = "RaisePropertyChanged received a invalid property name : " + propertyName;
				throw new Exception(msg);
			}
		}


	}
}
